/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Extractor.

    Maxsi Extractor is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Extractor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Extractor.  If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiExtractorContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiExtractorContributors.h. For more information please see
	MaxsiExtractorContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]


	MaxsiExtractor.exe
	A self-extracting application able to store a series of files inside it
	and able to extract these files to the current working directory, run the
	files, and once done, delete all the files.

	UI.cpp
	The User Interface for MaxsiExtractor

****************************************************************************/

#include "MaxsiEngine.h"
#include <io.h>
#include <direct.h>
#include <iostream> 
#include <fcntl.h>
#include <SYS\Stat.h>
#include "Main.h" 
#include "UI.h" 
#include "Data.h" 

PageExtractor::PageExtractor()
{

}

PageExtractor::~PageExtractor()
{

}

bool PageExtractor::SetProgress(int Progress)
{
	if (!Initialized) { return false; }
	if (!ProgressBar) { return false; }

	SendMessage(ProgressBar,PBM_SETPOS,(WPARAM)Progress,(LPARAM)0);

	return true;
}


bool PageExtractor::InitializeForm(ME_Window* Parent)
{
	BaseClass::InitializeForm(Parent);

	char*	HeaderText;

	if ( ProductName )
	{
		HeaderText	=	BuildString(3,"Self-Extracting ",ProductName,"!");	
	}
	else
	{
		
		HeaderText	=	BuildString(1,"Self-Extracting!");
	}

	RECT ClientSpace;
	GetClientRect (WindowHWND, &ClientSpace);

	Header				=	CreateLabelA(Parent->WindowHWND,HeaderText,0,0,0,0);
	ProgressBar			=	CreateProgressBarA(Parent->WindowHWND,0,0,0,0,0);
	StatusLabel			=	CreateLabelW(Parent->WindowHWND,0,0,0,0,0);
	SubStatusLabel		=	CreateLabelW(Parent->WindowHWND,0,0,0,0,0);
	
	delete[] HeaderText;
	
	NewResolution((WORD)ClientSpace.right,(WORD)ClientSpace.bottom);


	return	true;
}

bool PageExtractor::RenderForm(HWND hWnd)
{	
	if (!Initialized) { return false; }
    PAINTSTRUCT ps          = {0};
	HDC hDC = BeginPaint (hWnd, &ps);

	RECT rectClient;

    GetClientRect (hWnd, &rectClient);

	DrawSplitLine(hDC,50,rectClient.right);
	DrawSplitLine(hDC,rectClient.bottom-2.5*24,rectClient.right);
	
	EndPaint (hWnd, &ps);

	return true;
}


LRESULT PageExtractor::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (!Initialized) { return 0; }

	if ( message == WM_COMMAND )
	{
        switch(LOWORD(wParam)) 
        {
			case MSG_SetStatus:
			{			
				SendMessage(StatusLabel,WM_SETTEXT,(WPARAM)lParam,0);
				delete[] (char*)lParam;
			}
			break;
			case MSG_SetSubStatus:
			{
				SendMessage(SubStatusLabel,WM_SETTEXT,(WPARAM)lParam,0);
				delete[] (char*)lParam;
			}
			break;		
			case MSG_SetProgress:
				SetProgress((int)lParam);
			break;
			case QuitMessage:
				PostQuitMessage(0);
			break;
		}
	}
	else
	{
		return BaseClass::WindowProc(hWnd,message,wParam,lParam);
	}
    return (0);

}

bool PageExtractor::TerminateForm()
{
	// Avoid calling this function twice, or worse,
	// if one of the functions called here invokes another
	// windows message that eventually leads to this point of code again.
	if (!Initialized) { return false; }
	Initialized	=	false;

	DestroyWindow(ProgressBar);
	DestroyWindow(StatusLabel);
	DestroyWindow(SubStatusLabel);
	DestroyWindow(Header);

	// If we are here, we are safe. Call the parent and ask him to continue our work.
	// Make sure he sets Initialized to false afterwards.
	Initialized	=	true;

	BaseClass::TerminateForm();
	return true;
}

bool PageExtractor::NewResolution( WORD Width, WORD Height)
{	
	MoveWindow(ProgressBar,25,85,Width-25-25,18,TRUE);
	MoveWindow(StatusLabel,25,55,Width-25-25,85-55,TRUE);
	MoveWindow(SubStatusLabel,25,110,Width-25-25,Height-110-3*24,TRUE);
	MoveWindow(Header,25,25,Width-25-25,24,TRUE);

	RECT r;
	r.bottom=Height;
	r.top=0;
	r.left=0;
	r.right=Width;
	RedrawWindow(WindowHWND,&r,0, RDW_INVALIDATE | RDW_NOINTERNALPAINT | RDW_NOCHILDREN | RDW_ERASE);
	
	return true;
}